#include <stdio.h>            
#include <stdlib.h>          
#include <string.h>         
#include <unistd.h>          
#include <arpa/inet.h>     
  
 
#define PORT 8080  
  
int main() {  
    int sock = 0;                                 
    struct sockaddr_in serv_addr;   
    char buffer[1024] = {0};             
  
    // 创建一个socket  ，IPv4地址族，TCP协议
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {  
        printf("\n Socket creation error \n"); 
        return -1;   
    }  
  
    // 设置服务器地址信息  
    serv_addr.sin_family = AF_INET;    
    serv_addr.sin_port = htons(PORT);   
  
    // 将字符串地址转换为网络地址结构  
    if (inet_pton(AF_INET, "192.168.137.15", &serv_addr.sin_addr) <= 0) {  
        printf("\nInvalid address/ Address not supported \n");   
        return -1;  
    }  
  
    // 连接到服务器  
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {  
        printf("\nConnection Failed \n");  
        return -1; 
    }  
  
    // 循环接收用户输入并发送给服务器  
    while (1) {  
        printf("Enter command (on/off/blink <interval>): "); 
        fgets(buffer, 1024, stdin); // 从标准输入读取用户输入的命令 
        buffer[strcspn(buffer, "\n")] = 0;  
  
        // 发送命令到服务器  
        send(sock, buffer, strlen(buffer), 0);  
        printf("Command sent: %s\n", buffer); 
    }  

    close(sock);  
    return 0;  
}